package com.crb.smartcard.utils;

import android.annotation.SuppressLint;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

public class NetUtil {
    public static final String NETWORN_NONE = "UNCONNECTED";
    public static final String NETWORN_WIFI = "WIFI";
    public static final String NETWORN_2G = "2G";
    public static final String NETWORN_3G = "3G";
    public static final String NETWORN_4G = "4G";
    public static final String NETWORN_MOBILE = "NETWORK_MOBILE";

    public NetUtil() {
    }


    public static String getNetProvider() {
        String ProvidersName = "";
        @SuppressLint("WrongConstant") TelephonyManager telManager = (TelephonyManager) Utils.getApp().getApplicationContext().getSystemService("phone");
        String operator = telManager.getSimOperator();
        LogUtil.e("operator:" + operator);
        if (operator != null) {
            if (!"46000".equals(operator) && !"46002".equals(operator) && !"46007".equals(operator)) {
                if ("46001".equals(operator)) {
                    ProvidersName = "中国联通";
                } else if ("46003".equals(operator)) {
                    ProvidersName = "中国电信";
                }
            } else {
                ProvidersName = "中国移动";
            }
        }

        return ProvidersName;
    }

    @SuppressLint({"MissingPermission"})
    public static String getNetworkType() {
        @SuppressLint("WrongConstant") ConnectivityManager connManager = (ConnectivityManager) Utils.getApp().getApplicationContext().getSystemService("connectivity");
        if (null == connManager) {
            return "UNCONNECTED";
        } else {
            NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
            if (activeNetInfo != null && activeNetInfo.isAvailable()) {
                NetworkInfo wifiInfo = connManager.getNetworkInfo(1);
                if (null != wifiInfo) {
                    NetworkInfo.State state = wifiInfo.getState();
                    if (null != state && (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING)) {
                        return "WIFI";
                    }
                }

                NetworkInfo networkInfo = connManager.getNetworkInfo(0);
                if (null != networkInfo) {
                    NetworkInfo.State state = networkInfo.getState();
                    String strSubTypeName = networkInfo.getSubtypeName();
                    if (null != state && (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING)) {
                        switch (activeNetInfo.getSubtype()) {
                            case 1:
                            case 2:
                            case 4:
                            case 7:
                            case 11:
                                return "2G";
                            case 3:
                            case 5:
                            case 6:
                            case 8:
                            case 9:
                            case 10:
                            case 12:
                            case 14:
                            case 15:
                                return "3G";
                            case 13:
                                return "4G";
                            default:
                                if (!"TD-SCDMA".equalsIgnoreCase(strSubTypeName) && !"WCDMA".equalsIgnoreCase(strSubTypeName) && !"CDMA2000".equalsIgnoreCase(strSubTypeName)) {
                                    return "NETWORK_MOBILE";
                                }

                                return "3G";
                        }
                    }
                }

                return "UNCONNECTED";
            } else {
                return "UNCONNECTED";
            }
        }
    }
}
